package com.hww.doc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hww.doc.business.annotation.RedisCache;
import com.hww.doc.business.entity.Article;
import com.hww.doc.business.entity.Itemsets;
import com.hww.doc.business.entity.Tags;
import com.hww.doc.business.service.DocItemsetService;
import com.hww.doc.business.vo.ItemsetsConditionVO;
import com.hww.doc.persistence.beans.*;
import com.hww.doc.persistence.mapper.*;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 空间列表
 *
 * @author yaxu.huang
 * @version 1.0
 * @website http://www.fczd.online
 * @date 2018/4/16 16:26
 * @since 1.0
 */
@Slf4j
@Service
public class DocItemsetServiceImpl implements DocItemsetService {

	@Autowired
	private DocItemsetsMapper docItemsetsMapper;
	
	@Autowired
	private DocBooksMapper docBooksMapper;

	/**
	 * 分页查询
	 *
	 * @param vo
	 * @return
	 */
	@Override
	public PageInfo<Itemsets> findPageBreakByCondition(ItemsetsConditionVO vo) {
		PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
		List<DocItemsets> list = docItemsetsMapper.findPageBreakByCondition(vo);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		List<Itemsets> boList = new LinkedList<>();
		for (DocItemsets docItemsets : list) {
			boList.add(new Itemsets(docItemsets));
		}
		PageInfo bean = new PageInfo<DocItemsets>(list);
		bean.setList(boList);
		return bean;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@RedisCache(flush = true)
	public Itemsets insert(Itemsets entity) {
		Assert.notNull(entity, "Itemset不可为空");
		// 空间创建\修改默认只有超级用户root可以创建
		entity.setMemberId(1);
		entity.setModifyAt(1);
		entity.setCreateTime(new Date());
		docItemsetsMapper.insertSelective(entity.getDocItemsets());
		return entity;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@RedisCache(flush = true)
	public boolean removeByPrimaryKey(Long primaryKey) {
		boolean result = docItemsetsMapper.deleteByPrimaryKey(primaryKey) > 0;
		// 空间删除同步删除项目
		Example booksExample = new Example(DocBooks.class);
		Example.Criteria booksCriteria = booksExample.createCriteria();
		booksCriteria.andEqualTo("itemId", primaryKey);
		docBooksMapper.deleteByExample(booksExample);
		return result;
	}

	@Override
	@RedisCache(flush = true)
	public boolean updateSelective(Itemsets entity) {
		Assert.notNull(entity, "Itemset不可为空！");
        entity.setUpdateTime(new Date());
        return docItemsetsMapper.updateByPrimaryKeySelective(entity.getDocItemsets()) > 0;
	}

	@Override
	public Itemsets getByPrimaryKey(Long primaryKey) {
		Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        DocItemsets entity = docItemsetsMapper.selectByPrimaryKey(primaryKey);
        return null == entity ? null : new Itemsets(entity);
	}
}
